﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class ProjectController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public ProjectController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            JwtSettings jwtSettings = new JwtSettings();
            jwtSettings.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwtSettings.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwtSettings.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwtSettings;
            //_jwtSettings = jwtSettingsAccesser.Value;
            _access = access;
        }

        /// <summary>
        /// 新建项目信息
        /// </summary>
        [HttpPost]
        [Route("AddProject")]
        public ResponseMessage AddProject(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            ProjectDAL da = new ProjectDAL();
            try
            {
                if (projectInfo.project_name.Length > 50)
                {
                    message.errcode = 1;
                    message.errmsg = "项目名称不能大于50个字符";

                }
                else if (projectInfo.project_addr.Length > 100)
                {
                    message.errcode = 1;
                    message.errmsg = "项目地址不能大于100个字符";
                }
                else
                {
                    int isdefault;

                    string code = string.Empty;
                    bool result = true;
                    while (result)
                    {
                        Random random = new Random();
                        code = random.Next(1, 255).ToString();
                        result = da.CheckProID(code);
                    }
                    projectInfo.project_code = code;
                    projectInfo.MenuRole = "5,6,7,11,13,14,15,23,16,17,18,19,20,21,22";
                    message = BUS.AddProject(projectInfo, out isdefault);
                    if (isdefault == 1 && HttpContext.Session.GetString("LoginName") == "fstar")
                    {
                        ProjectInfo info = da.QueryDefaultProject();
                        message.data = info;
                        int project = info.ID;
                        HttpContext.Session.SetString("ProjectID", project.ToString());
                    }
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增项目异常";
            }
            return message;
        }

        /// <summary>
        /// 修改项目信息
        /// </summary>
        [HttpPost]
        [Route("EditProject")]
        public ResponseMessage EditProject(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            try
            {
                message = BUS.EditProject(projectInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "修改项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "修改项目信息异常";
            }
            return message;
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        [HttpPost]
        [Route("DeleteProject")]
        public ResponseMessage DeleteProject(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            ProjectDAL da = new ProjectDAL();
            try
            {
                int project = da.QueryDefaultProject().ID;
                if (project == projectInfo.ID)
                {
                    int resetproject = da.QueryMinProjectID(project);
                    bool ret = da.SwitchProject(resetproject);
                    HttpContext.Session.SetString("ProjectID", resetproject.ToString());
                }

                message = BUS.DeleteProject(projectInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除项目异常";
            }
            return message;
        }

        /// <summary>
        /// 查询项目
        /// </summary>
        [HttpPost]
        [Route("QueryProject")]
        public ResponseMessage QueryProject(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            try
            {
                string username = HttpContext.Session.GetString("LoginName");
                int projectid = Convert.ToInt32(HttpContext.Session.GetString("ProjectID"));
                message = BUS.QueryProject(projectInfo, username, projectid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询项目异常";
            }
            return message;
        }

        /// <summary>
        /// 载入项目信息
        /// </summary>
        [HttpPost]
        [Route("LoadProject")]
        public ResponseMessage LoadProject(ProjectInfo projectInfo)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            try
            {
                message = BUS.LoadProject(projectInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "载入项目";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "载入项目信息异常";
            }
            return message;
        }

        /// <summary>
        /// 查询应用方式
        /// </summary>
        [HttpPost]
        [Route("QueryApplyType")]
        public ResponseMessage QueryApplyType()
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            try
            {
                message = BUS.QueryApplyType();
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询应用方式";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询应用方式异常";
            }
            return message;
        }

        /// <summary>
        /// 绑定菜单
        /// </summary>
        [HttpPost]
        [Route("BindMenu")]
        public ResponseMessage BindMenu(string menu, int proid)
        {
            ResponseMessage message = new ResponseMessage();
            ProjectBUS BUS = new ProjectBUS();
            try
            {
                message = BUS.BindMenu(menu, proid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "绑定菜单";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "绑定菜单异常";
            }
            return message;
        }
    }
}
